package org.tt.sudoku;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
//import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class SudokuView extends View {
	
	//instansvariabler
	private float tileWidth; 
	private float tileHeight;
	
	private int tileSelectionX;
	private int tileSelectionY;
	
	private final Rect tile = new Rect();
	
	
	private final Sudoku sudoku;
	
	//Konstruktor
	
	public SudokuView(Context context) {
		super(context);
		
		this.sudoku = (Sudoku) context;
		setFocusable(true);
		setFocusableInTouchMode(true);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh){
		tileWidth = w/9.0f;
		tileHeight = h/9.0f;
		getTile(tileSelectionX, tileSelectionY, tile);
		super.onSizeChanged(w, h, oldw, oldh);
	}
	
	private void getTile(int x, int y, Rect tile){
		tile.set((int) (x * tileWidth), (int) (y * tileHeight), 
			(int) (x * tileWidth + tileWidth), (int) (y * tileHeight + tileHeight));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		//320x480
		Paint background = new Paint();
		Paint groupColor = new Paint();
		background.setColor(getResources().getColor(R.color.sudoku_background));
		groupColor.setColor(getResources().getColor(R.color.sudoku_groupbackground));
		
		//Draw background
		canvas.drawRect(0,0,getWidth(),getHeight(),background);
		
		//Draw top group
		canvas.drawRect(0,0,getWidth()/3.0f,getHeight()/3.0f,groupColor);
		canvas.drawRect(getWidth()-getWidth()/3.0f,0,getWidth(),getHeight()/3.0f,groupColor);
		
		//Draw center group
		canvas.drawRect(getWidth()/3.0f,getHeight()/3.0f,getWidth()-getWidth()/3.0f,getHeight()-getHeight()/3.0f,groupColor);
		
		//Draw bottom group
		canvas.drawRect(0,getHeight()-getHeight()/3.0f,getWidth()/3.0f,getHeight(),groupColor);
		canvas.drawRect(getWidth()-getWidth()/3.0f,getHeight()-getHeight()/3.0f,getWidth(),getHeight(),groupColor);
		
		//Draw lines
		Paint darkLine = new Paint();
		darkLine.setColor(getResources().getColor(R.color.sudoku_dark));
		
		Paint hightlightLine = new Paint();
		hightlightLine.setColor(getResources().getColor(R.color.sudoku_hilite));
		Paint lightLine = new Paint();
		lightLine.setColor(getResources().getColor(R.color.sudoku_light));
		
		// Draw the minor grid lines
		for (int i = 0; i < 9; i++) {
			canvas.drawLine(0, i * tileHeight, getWidth(), i * tileHeight,
			lightLine);
			canvas.drawLine(0, i * tileHeight + 1, getWidth(), i * tileHeight
			+ 1, hightlightLine);
			canvas.drawLine(i * tileWidth, 0, i * tileWidth, getHeight(),
			lightLine);
			canvas.drawLine(i * tileWidth + 1, 0, i * tileWidth + 1,
			getHeight(), hightlightLine);
		}
	
		// Draw the major grid lines
		for (int i = 0; i < 9; i++) {
			if (i % 3 != 0)
				continue;
			canvas.drawLine(0, i * tileHeight, getWidth(), i * tileHeight,
					darkLine);
			canvas.drawLine(0, i * tileHeight + 1, getWidth(), i * tileHeight
			+ 1, hightlightLine);
			canvas.drawLine(i * tileWidth, 0, i * tileWidth, getHeight(), darkLine);
			canvas.drawLine(i * tileWidth + 1, 0, i * tileWidth + 1,
			getHeight(), hightlightLine);
		}
		
		// Draw numbers
		Paint numbers = new Paint(Paint.ANTI_ALIAS_FLAG);
		numbers.setColor(getResources().getColor(R.color.sudoku_foreground));
		numbers.setStyle(Style.FILL);
		numbers.setTextSize(tileHeight * 0.5f);
		numbers.setTextScaleX(tileWidth / tileHeight);
		numbers.setTextAlign(Paint.Align.CENTER);
		// Draw the number in the center of the tile
		FontMetrics fm = numbers.getFontMetrics();
		// Centering in X: use alignment (and X at midpoint)
		float x = tileWidth / 2;
		// Centering in Y: measure ascent/descent first
		float y = tileHeight / 2 - (fm.ascent + fm.descent) / 2;
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				String number = sudoku.getTileString(i, j);
				//int offset = j*9;
				//String number = sudoku.easySudoku.substring(i+offset,i+1+offset);
				if(number.compareTo("0")!=0)
					canvas.drawText(number, i * tileWidth + x, j * tileHeight + y, numbers);
			}
		}
		
		//Color selection
		Paint selectedTile = new Paint();
		selectedTile.setColor(getResources().getColor(R.color.sudoku_selected));
		canvas.drawRect(tile, selectedTile);
	}
	
	//Select tile
	
	private void tileSelection(int x, int y){
		invalidate(tile);
		tileSelectionX = Math.min(Math.max(x, 0), 8);
		tileSelectionY = Math.min(Math.max(y, 0), 8);
		getTile(tileSelectionX, tileSelectionY, tile);
		invalidate(tile);
	}
	public void setSelectedTile(int value) {
		sudoku.setTile(tileSelectionX, tileSelectionY, value);
		invalidate();
		
	}
	
	@Override
	public boolean onKeyDown(int key, KeyEvent event)
	{
		
		switch(key) {
		case KeyEvent.KEYCODE_DPAD_UP:
			tileSelection(tileSelectionX,tileSelectionY-1);
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			tileSelection(tileSelectionX,tileSelectionY+1);
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			tileSelection(tileSelectionX-1,tileSelectionY);
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			tileSelection(tileSelectionX+1,tileSelectionY);
			break;
			
		case KeyEvent.KEYCODE_SPACE: setSelectedTile(0); break;	
		case KeyEvent.KEYCODE_1: setSelectedTile(1); break;
		case KeyEvent.KEYCODE_2: setSelectedTile(2); break;
		case KeyEvent.KEYCODE_3: setSelectedTile(3); break;
		case KeyEvent.KEYCODE_4: setSelectedTile(4); break;
		case KeyEvent.KEYCODE_5: setSelectedTile(5); break;
		case KeyEvent.KEYCODE_6: setSelectedTile(6); break;
		case KeyEvent.KEYCODE_7: setSelectedTile(7); break;
		case KeyEvent.KEYCODE_8: setSelectedTile(8); break;
		case KeyEvent.KEYCODE_9: setSelectedTile(9); break;
			
		}
		return true;
		
	}
	@Override
	public boolean onTouchEvent(MotionEvent touchEvent){
		if(touchEvent.getAction() != MotionEvent.ACTION_DOWN)
		{
			return super.onTouchEvent(touchEvent);
		}
		
		tileSelection((int) (touchEvent.getX()/ tileWidth), (int) (touchEvent.getY()/ tileHeight));
		return true;
	}
	
}
